# -*- coding: utf-8 -*-
# @Time    : 2024/11/29  14:28
# @Author  : Nabby
# @FileName: jasco_order_impl.py
# @Desc    :


from app import db
from infrastructure.po.mode.jasco_order import JascoOrder
from sqlalchemy import and_, or_, desc, asc


class JascoOrderImpl(object):
    @classmethod
    def get_jasco_order_list(
        cls,
        user_id,
        order_code=None,
        logistics_code=None,
        destination=None,
        consignee=None,
        postcode=None,
        keyword=None,
        page=1,
        page_size=999,
    ):
        """
        查询jasco订单列表
        :param page: 页码
        :param page_size: 每页数量
        :return:
        """
        query = db.session.query(JascoOrder)
        if order_code:
            query = query.filter(JascoOrder.code == order_code)
        if logistics_code:
            query = query.filter(JascoOrder.logistics_code == logistics_code)
        if destination:
            query = query.filter(JascoOrder.destination == destination)
        if consignee:
            query = query.filter(JascoOrder.consignee == consignee)
        if postcode:
            query = query.filter(JascoOrder.postcode == postcode)
        # if keyword:
        #     query = query.filter(JascoOrder.keyword == keyword)
        if user_id != "Jasco":
            query = query.filter(JascoOrder.user_id == user_id)
        query = query.order_by(desc(JascoOrder.create_time))
        return query.paginate(page=page, per_page=page_size), query.count()

    @classmethod
    def get_jasco_order_by_id(cls, id):
        """
        根据id查询jasco订单
        :param id:
        :return:
        """
        return db.session.query(JascoOrder).filter(JascoOrder.id == id).first()

    @classmethod
    def edit_cargo_detail_overview(cls, id, packages_num, packages_weight, packages_volume, billing_weight_squaremeter, volume_divisor):
        """
        根据id修改jasco订单
        :param id:
        :param cargo_detail_overview:
        :return:
        """
        jasco_order = db.session.query(JascoOrder).filter(JascoOrder.id == id).first()
        if not jasco_order:
            return False
        jasco_order.packages_num = packages_num
        jasco_order.packages_weight = packages_weight
        jasco_order.packages_volume = packages_volume
        jasco_order.billing_weight_squaremeter = billing_weight_squaremeter
        jasco_order.volume_divisor = volume_divisor
        db.session.commit()
        return True


    @classmethod
    def add_jasco_order(
        cls,
        user_id,
        order_code,
        status=None,
        type=None,
        logistics_code=None,
        logistics_channel=None,
        receiving_time=None,
        consignee=None,
        consignee_company=None,
        consignee_phone=None,
        consignee_email=None,
        postcode=None,
        destination=None,
        has_insure=None,
        # keyword=None,
        clearance_type=None,
        trade_clause=None,
        tfn_vat=None,
        tfn_eori=None,
        tfn_ein=None,
        tfn_other=None,
        packages_num=None,
        address1=None,
        address2=None,
        address3=None,
        packages_weight=None,
        packages_volume=None,
        billing_weight_squaremeter=None,
        volume_divisor=None
    ):
        count = (
            db.session.query(JascoOrder).filter(JascoOrder.code == order_code).count()
        )
        if count > 0:
            return False
        else:
            jasco_order = JascoOrder(
                user_id=user_id,
                code=order_code,
                status=status,
                type=type,
                logistics_code=logistics_code,
                logistics_channel=logistics_channel,
                receiving_time=receiving_time,
                consignee=consignee,
                consignee_company=consignee_company,
                consignee_phone=consignee_phone,
                consignee_email=consignee_email,
                postcode=postcode,
                destination=destination,
                has_insure=has_insure,
                # keyword=keyword,
                clearance_type=clearance_type,
                trade_clause=trade_clause,
                tfn_vat=tfn_vat,
                tfn_eori=tfn_eori,
                tfn_ein=tfn_ein,
                tfn_other=tfn_other,
                packages_num=packages_num,
                address1=address1,
                address2=address2,
                address3=address3,
                packages_weight=packages_weight,
                packages_volume=packages_volume,
                billing_weight_squaremeter=billing_weight_squaremeter,
                volume_divisor=volume_divisor
            )
            db.session.add(jasco_order)
            db.session.flush()
            db.session.commit()
            return True

    @classmethod
    def edit_jasco_order(
        cls,
        id,
        order_code,
        status=None,
        type=None,
        logistics_code=None,
        logistics_channel=None,
        receiving_time=None,
        consignee=None,
        consignee_company=None,
        consignee_phone=None,
        consignee_email=None,
        postcode=None,
        destination=None,
        has_insure=None,
        # keyword=None,
        clearance_type=None,
        trade_clause=None,
        tfn_vat=None,
        tfn_eori=None,
        tfn_ein=None,
        tfn_other=None,
        packages_num=None,
        address1=None,
        address2=None,
        address3=None,
        packages_weight=None,
        packages_volume=None,
        billing_weight_squaremeter=None,
        volume_divisor=None
    ):
        jasco_order = db.session.query(JascoOrder).filter(JascoOrder.id == id).first()
        if not jasco_order:
            return False
        jasco_order.code = order_code
        jasco_order.status = status
        jasco_order.type = type
        jasco_order.logistics_code = logistics_code
        jasco_order.logistics_channel = logistics_channel
        jasco_order.receiving_time = receiving_time
        jasco_order.consignee = consignee
        jasco_order.consignee_company = consignee_company
        jasco_order.consignee_phone = consignee_phone
        jasco_order.consignee_email = consignee_email
        jasco_order.postcode = postcode
        jasco_order.destination = destination
        jasco_order.has_insure = has_insure
        # jasco_order.keyword = keyword
        jasco_order.clearance_type = clearance_type
        jasco_order.trade_clause = trade_clause
        jasco_order.tfn_vat = tfn_vat
        jasco_order.tfn_eori = tfn_eori
        jasco_order.tfn_ein = tfn_ein
        jasco_order.tfn_other = tfn_other
        jasco_order.packages_num = packages_num
        jasco_order.address1 = address1
        jasco_order.address2 = address2
        jasco_order.address3 = address3
        jasco_order.packages_weight = packages_weight
        jasco_order.packages_volume = packages_volume
        jasco_order.billing_weight_squaremeter = billing_weight_squaremeter
        jasco_order.volume_divisor = volume_divisor
        db.session.commit()
        return True

    @classmethod
    def delete_jasco_order(cls, id):
        """
        根据id删除jasco订单
        :param id:
        :return:
        """
        jasco_order = db.session.query(JascoOrder).filter(JascoOrder.id == id).first()
        if not jasco_order:
            return False
        db.session.delete(jasco_order)
        db.session.commit()



